$(function() {

    var datehtmlStr = '<label for="query_start_date0"> <input id="query_start_date0" placeholder="2017/11/29" name="query_start_date[]" value="" type="text" class="s-input-txt openrili_start"/> <img class="y" src="/image/officeHall/uploadAssetPackage/data.png"/> </label> <span> —</span> <label for="query_end_date0"> <input id="query_end_date0" placeholder="2017/11/29" name="query_end_date" value="" type="text" class="s-input-txt openrili_end"/> <img class="y" src="/image/officeHall/uploadAssetPackage/data.png"/> </label><span class="endImmediately">        <input type="text" id="endImmediately" class="workeNode"   placeholder="完成新包尽调评估报告"/>        </span> <span> <input type="text" hidden class="upPlanWork" value="0"/> </span><button type="button" class="btn btn-success">添加        </button> <button type="button" class="btn btn-danger"> 删除        </button>';

    //发起协同1团队组成*********************
//  点击关闭按钮
    //点击上一步
    $('.lastStep').click(function(){
        $('.maskTwo').hide();
    })
    //点击下一步
    $('.nextStep').click(function(){
        $('.maskTwo').show();
        $("#nextButton").css('display','none');
        $('#mark').attr('display','none');
    })
    //输入框获取焦点
    $('.formOneUl .inputText').focus(function(){
        $(this).siblings().show();
        $(this).parent().siblings().find('.seachBox').hide();
    })
    //   搜索框获取焦点
    $('.formOneUl .inputHider').focus(function(){
        var seach = $(this).parent().find('input').val();
        var th = $(this);
        //按下回车键事件
        document.onkeydown = function(e){
            var ev = document.all ? window.event : e;
            if(13 == ev.keyCode ) {
                var seach = th.parent().find('input').val();
                var inputText = th.parent().siblings('input').val();
                if(inputText.trim() != ''){
                    inputText = inputText + ",";
                }
                if(seach.trim() != ''){
                    inputText += seach;
                    th.parent().find('input').val('');
                    th.parent().siblings('input').val(inputText);
                }

                th.parent().hide();
            }
        }


    })

//  $('.formOneUl .inputText').blur(function(){
//      $(this).siblings().find('input').blur(function(){
//          $(this).parent().hide();
//      })
//  })
//

    $('.seachBox .determine').on('click',function(){
        var idVersel = $(this).parent().parent().prev().val();
        if (idVersel==''){
            UserId = ',';
        }
        var seach = $(this).parent().find('input').val();
        //判断用户是否正确/并且获取id中
        //获取对应id
        for(var tmp in JsonToUserData){
            if ($.inArray(JsonToUserData[tmp]['username'],JsonToUserData)){
                if (JsonToUserData[tmp]['username'] == seach){
                    UserId +=JsonToUserData[tmp]['id']+',';
                }
            }else{
                alert('用户不存在')
            }
        }
        $(this).parent().parent().prev().val(UserId)
        var inputText = $(this).parent().siblings('input').val();
        if(inputText.trim() != ''){
            inputText = inputText +  ",";
        }
        if(seach.trim() != ''){
            inputText += seach;
            $(this).parent().find('input').val('');
            $(this).parent().siblings('input').val(inputText);
        }

        $(this).parent().hide();
    })





    //发起协同2工作计划部分*********************
    //单击添加
    var datelenght = $('.maskTwoCenter ul li label').length/2+1;
    //          <!--单击添加-->
    $('.maskTwoCenter  ul').on('click','li .btn-success',function(){
        datelenght = $('.maskTwoCenter ul li label').length/2+1;
        var className = 'date'+datelenght;
        var jqId = '#'+className;
        var str = '<li id='+className+'>'+datehtmlStr+"</li>";
        var  start = 'query_start_date'+datelenght;
        var  end = 'query_end_date'+datelenght;
        $("#dataUl").append(str);
        $(jqId).children("label").eq(0).attr('for',start);
        $(jqId).children("label").eq(1).attr('for',end);
        $(jqId).children("label:first-child").find('input').attr('id',start);
        $(jqId).children("label").eq(1).find('input').attr('id',end);
        $(jqId).children("label").eq(1).children('input').val('');
        $(jqId).children("span").eq(1).children('input').val(''); //清空文本内容
        $(jqId).children("span").eq(2).children('input').attr('value',0)
    })
    //          <!--//  动态启动日期方法-->

    $('.maskTwoCenter ul').on('click','.openrili_start',function(){
        var str = $(this).attr('id');
        $('#'+str).datepicker();
    })
    //给所有日期选择添加事件
    $('.maskTwoCenter ul').on('click','.openrili_end',function(){
        var str = $(this).attr('id');
        $('#'+str).datepicker();
    })

    //          <!--单击删除-->
    $('.maskTwoCenter ul').on('click','.btn-danger',function(){
        if ($(this).parent().parent().find('li').length == 1 ){
            layer.msg("最后一个禁止删除");
        }else{
            $(this).parent().remove();
        }
    })

    //兼容火狐、IE8
    //显示遮罩层
    function showMask(){
        $(".modalFrame").height($(document).height());
        $(".modalFrame").width($(document).width());
        $(".modalFrame").show();
        $('.mask').toggle('display');
    }

    //隐藏遮罩层
    function hideMask(){
        $("modalFrame").hide();
        $('.mask').hide();
    $(".modalFrame").height(0);
   }

    //点击提交按钮
    $("#submit").on('click',function () {
        var yu = $('.select4').select2('data');
        var yuArr = [];
        $.each(yu,function(i,j){
            //i为元素的索引，从0开始,j为当前处理的元素对象
            yuArr.push(j.text);
        });
        var jin = $('.select3').select2('data');
        var jinArr = [];
        $.each(jin,function(i,j){
            //i为元素的索引，从0开始,j为当前处理的元素对象
            jinArr.push(j.text);
        });
        var leader = $('#realname').select2('data');
        var leaderArr = [];
        $.each(leader,function(i,j){
            //i为元素的索引，从0开始,j为当前处理的元素对象
            leaderArr.push(j.text);
        });
        var zongjian = $('#zongjian').select2('data');
        var zongjianArr = [];
        $.each(zongjian,function(i,j){
            //i为元素的索引，从0开始,j为当前处理的元素对象
            zongjianArr.push(j.text  );
        });
        //1.拼装数据
        //------团队成员信息----
        var teamMembersDataArr	=	{};
        teamMembersDataArr['id'] 					= $('#package_plan_id').val();				//通过get获取//判断这个资产包基础表的id是否存在，存在则更新，不存在则添加
        teamMembersDataArr['package_id'] 			= $('#package_id').val();				//通过get获取
        teamMembersDataArr['uploaded_by_id']  	= $('#uploaded_by_id').val();
        teamMembersDataArr['uploaded_by_name']   = $('#uploaded_by_name').val();
        teamMembersDataArr['yuArr']			= yuArr;
        teamMembersDataArr['jinArr']			= jinArr;
        teamMembersDataArr['leaderArr']			= leaderArr;
        teamMembersDataArr['zongjianArr']			= zongjianArr;
        // teamMembersDataArr['leader_id']  			= $('#leader_id').val();
        // teamMembersDataArr['leader_name']  		= $('#leader_name').val();
        // teamMembersDataArr['pre_evaluation_id']  = $('#pre_evaluation_id').val();
        // teamMembersDataArr['pre_evaluation_name']  = $('#pre_evaluation_name').val();
        // teamMembersDataArr['due_diligence_id']  	= $('#due_diligence_id').val();
        // teamMembersDataArr['due_diligence_name']  = $('#due_diligence_name').val();
        teamMembersDataArr['other_team_notes']  	= $('#other_team_notes').val();
        //------工作计划信息----
        // $("#dataUl li").each(function(){
        // 	console.log($(this).first().first().html());
        // });
        var workPlanNum = $('#dataUl li').length;
        var workPlanDataArr = {};
        for(var i = 0; i < workPlanNum; i++){
            var packageID		= $('#package_id').val();
            var workPlanStartID =  $("#dataUl li").eq(i).first().find('input').val();
            var workPlanEndID 	=  $("#dataUl li").eq(i).children().eq(2).find('input').val();
            var workPlanVal 	=  $("#dataUl li").eq(i).children().eq(3).find('input').val()!=undefined?$("#dataUl li").eq(i).children().eq(3).find('input').val():'';
            var upPlanWork 	=  $("#dataUl li").eq(i).children().eq(4).find('input').val()!=undefined?$("#dataUl li").eq(i).children().eq(4).find('input').val():0;
            var stage =  workPlanStartID+'/'+workPlanEndID;
            workPlanDataArr[i] = {};
            workPlanDataArr[i]['package_id'] = packageID;
            workPlanDataArr[i]['stage'] = stage;
            workPlanDataArr[i]['job'] = workPlanVal;
            workPlanDataArr[i]['id'] = upPlanWork;		////通过get获取   不是0则添加数据 ，是0则更新数据
        }
//2.post数据
        paramsData = {
            teamMembersData: teamMembersDataArr,
            workPlanData:workPlanDataArr
        };
        var package_id		= $('#package_id').val();
        $.post("/package-initiation-plan/set-package-team-work-data",paramsData,function(data){
            Jdata = JSON.parse(data)
            if (Jdata.code == 200){
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                layer.msg('保存成功');
                parent.location.reload("/package/detail-info?packageid="+package_id);
            }else{
                layer.msg('保存失败');
            }
        });
        function reJump() {
            var package_id		= $('#package_id').val();
            layer.open({
                type: 2,
                title: '修改协同',
                shadeClose: false,
                shade: 0,
                area: ['800px', '90%'],
                content: ['/package-initiation-plan/get-package-plan1?package_id='+package_id], //iframe的url
                success: function(layero, index){
//                                var index = parent.layer.getFrameIndex(window.name);
//                                layer.close(index)
                    layer.iframeAuto(index);
                }
            });
        }
    })

})
